/*
 * BookManagement.fx
 * Author:Yu Min-ting
 * Created on 2009-7-14, 14:52:16
 */

package com.sjtu.fx.admin;

import javafx.scene.*;


import com.sjtu.fx.tools.*;
import com.sjtu.fx.tools.SwingTable.*;

import javafx.scene.layout.VBox;


import javafx.scene.paint.Color;
import javafx.scene.text.Font;
import javafx.scene.text.Text;

import javafx.scene.layout.HBox;
import javafx.scene.control.Button;
import javafx.scene.control.Hyperlink;

import javafx.stage.Alert;

import javafx.scene.control.TextBox;

import javafx.io.http.HttpRequest;
import java.io.OutputStream;
import org.json.JSONObject;

import java.io.DataInputStream;
import java.io.InputStream;
import org.json.JSONArray;

import javafx.ext.swing.SwingComboBox;
import javafx.ext.swing.SwingComboBoxItem;

import java.lang.System;

public class Book{
    public var name:String;
    public var id:String;
    public var score:String;
    public var publisher:String;
    public var author:String;
}

public class BookManagement extends CustomNode {

   var panel:Panel;
    var books:Book[] = bind books with inverse;
    public var needScore:Number;
    public var selection: Integer;
    var addNewBooks:UpdateNewBooks;
    var idList:JSONArray;

    var bookTable:SwingTable = SwingTable{
        columns: [
                    TableColumn {
                        text: "Book Publisher"
                    },
                    TableColumn {
                        text: "Book Name"
                    },
                    TableColumn {
                        text: "Check in Score"
                    },
                    TableColumn {
                        text: "Author"
                    }
                ]
        rows: bind for (u in books)
                    TableRow{
                        cells:[
                            TableCell {
                                text:bind u.publisher
                            },
                            TableCell {
                                text:bind u.name
                            },
                            TableCell {
                                text: bind u.score
                            },
                             TableCell {
                                text:bind u.author
                            }
                        ]
                    }
        selection: bind selection with inverse

    }

    var scoreText = Text {
        font: Font {size:10 name: "Verdana"}
        fill: Color.rgb(11,51,60)
        content: "User Check in Score: "
    };
   var scoreTextBox = SwingComboBox {
            items: [
                SwingComboBoxItem {
                    text: "0"
                    selected: true
                }SwingComboBoxItem {
                    text: "1"
                }SwingComboBoxItem {
                    text: "2"
                }SwingComboBoxItem {
                    text: "3"
                }SwingComboBoxItem {
                    text: "4"
                }SwingComboBoxItem {
                    text: "5"
                }SwingComboBoxItem {
                    text: "6"
                }SwingComboBoxItem {
                    text: "7"
                }SwingComboBoxItem {
                    text: "8"
                }SwingComboBoxItem {
                    text: "9"
                }SwingComboBoxItem {
                    text: "10"
                }
            ]
        }

    var bookNameText = Text {
        font: Font {size:10 name: "Verdana"}
        fill: Color.rgb(11,51,60)
        content: "Book Name: "
    };
    var bookNameTextBox = TextBox {
            columns: 12
            selectOnFocus: true
        }
    var bookNameButton = Button {
            text: "Search"
            action: function() {
                if(bookNameTextBox.text != ""){
                    setBookResult(bookNameTextBox.text);
                }
            }
        }



    var scoreButton = Button {
            text: "Save Score Modification"
            action: function() {
                if(selection>=0){
                    books[selection].score = scoreTextBox.text;
                    updateScore(Integer.parseInt(idList.get(selection).toString()), scoreTextBox.text);
              //      System.out.println("id:{books[selection].id}");
              //      System.out.println("score:{books[selection].score}");
              //      setUserScore(books[selection].id,scoreLabel.text,"pass");
                    delete books[selection];
                }
            }
        }
    var alert = BookManageAlert {
         translateX:75
         translateY:200
         title:"Confirmaion"
         content:"Please input entire information"
         width:280
         height:120
         visible:false
         opacity:0.9
     }
    var alertInfrom = UIAlert {
         translateX:75
         translateY:200
         title:"ERROR"
         content:"Please input entire information"
         width:280
         height:120
         visible:false
         opacity:0.9

     }
    var deleteButton = Button {
            text: "Delete This Book"
            action: function() {
                if(selection>=0){
                     alert.content = "Make Sure to delete this book:\n {books[selection].name}";
                     alert.bm = this;
                     alert.bookId = Integer.parseInt(idList.get(selection).toString());
                     alert.visible = true;
                  
                 //    if(Alert.confirm("Confirmaion","Make Sure to delete this book:\n {books[selection].name}")){
                 //        deleteBook(Integer.parseInt(idList.get(selection).toString()));
                        
                  //   }
                }
            }
        }
    
    var addNewBook = Hyperlink {
            text: "Add New Book>>"
            action: function() {
                addNewBooks.visible = true;
            }
        }

    var details = Hyperlink {
            text: "See Details>>"
            action: function() {

            }
        }




    override protected function create() : Node {
 //       getUserCheckList();
        Group{
            content:[
                 panel = Panel {
                    width:    430
                    height:   550
                    title:    "Book Management"
                    translateX: 0
                    translateY: 0
                },
                VBox{
                    spacing: 25
                    translateX:32 translateY: 60
                    content:[
                        HBox{
                            spacing:25
                            content:[bookNameText,bookNameTextBox,bookNameButton]
                        }

                        bookTable,
                        HBox{
                            spacing:45
                            content:[scoreText,scoreTextBox]
                        },                      
                        HBox{
                            spacing:55
                            content:[ scoreButton, deleteButton]
                        },
                        details,addNewBook

                        ]

                }
                addNewBooks = UpdateNewBooks{
                    visible :false
                },alert,alertInfrom


            ]
        }
     }

     function setBookResult(bookName:String){
         HttpRequest {
        method:HttpRequest.POST
        location:"http://localhost:8764/JavaFXBack/Search"

       onOutput: function(os:OutputStream) {
            try {
                var json:JSONObject = JSONObject{};
                json.put("searchType","Book Name");
                json.put("KeyWord",bookName);
                var temp:String = "obj={json.toString()}";
         //       System.out.println(temp);
                os.write(temp.getBytes());
                os.flush();
            } finally {
                os.close();
            }
        }
        onInput: function(is: InputStream) {
        try {
                def data:DataInputStream = new DataInputStream(is);
                var json:JSONObject = JSONObject{};
                var result:String = data.readLine();
       //         System.out.println(result);
                json = new JSONObject(result);
                var bookNameList:JSONArray = json.getJSONArray("bookNameList");
                var authorList:JSONArray = json.getJSONArray("authorList");
                var publisherList:JSONArray = json.getJSONArray("publisherList");
                var urlList:JSONArray = json.getJSONArray("urlList");
                idList = json.getJSONArray("idList");
                var scoreList:JSONArray = json.getJSONArray("scoreList");
                var i:Integer = 0;
                while(i<bookNameList.length()){
                    books[i] = Book{
                            name:bookNameList.get(i).toString()
                            id:idList.get(i).toString()
                            score:scoreList.get(i).toString()
                            publisher:publisherList.get(i).toString()
                            author:authorList.get(i).toString()
                        };
                    i++;
                }
            } finally {
                is.close();
            }
     }
        }.start();
    }

     function updateScore(bookId:Integer,score:String){
            HttpRequest {
        method:HttpRequest.POST
        location:"http://localhost:8764/JavaFXBack/UpdateBookScore"

       onOutput: function(os:OutputStream) {
            try {
                var json:JSONObject = JSONObject{};
                json.put("bookId",bookId);
                json.put("score",score);
                var temp:String = "obj={json.toString()}";
         //       System.out.println(temp);
                os.write(temp.getBytes());
                os.flush();
            } finally {
                os.close();
            }
        }
        onInput: function(is: InputStream) {
        try {
                def data:DataInputStream = new DataInputStream(is);
                var json:JSONObject = JSONObject{};
                var result:String = data.readLine();
                alertInfrom.title = "Result";
                alertInfrom.content = result;
                alertInfrom.visible = true;
                //Alert.inform(result);
            } finally {
                is.close();
            }
     }
        }.start();
     }

     public function deleteBook(bookId:Integer){
          HttpRequest {
        method:HttpRequest.POST
        location:"http://localhost:8764/JavaFXBack/DeleteBook"
       onOutput: function(os:OutputStream) {
            try {
                var json:JSONObject = JSONObject{};
                json.put("BookId",bookId);
                var temp:String = "obj={json.toString()}";
         //       System.out.println(temp);
                os.write(temp.getBytes());
                os.flush();
            } finally {
                os.close();
            }
        }
        onInput: function(is: InputStream) {
        try {
                def data:DataInputStream = new DataInputStream(is);
                var json:JSONObject = JSONObject{};
                var result:String = data.readLine();
                if(result.equals("delete sucessfully")){
                    delete books[selection];
                }
                alertInfrom.title = "Result";
                alertInfrom.content = result;
                alertInfrom.visible = true;
               // Alert.inform(result);
            } finally {
                is.close();
            }
     }
        }.start();
     }
}
